﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.Windows;
using System.Windows.Media;

namespace BluSteamClient.View
{
    class ChatTab: TabItem
    {
        private CloseableHeader closableTabHeader = new CloseableHeader(); // Create an instance of the usercontrol

        public string tabTitle
        {
            get
            {
                return (((CloseableHeader)this.Header).FindName("label_TabTitle") as Label).Content.ToString(); //get label from xaml user control and set its cotent to the selected user;
            }
            set
            {
                (((CloseableHeader)this.Header).FindName("label_TabTitle") as Label).Content = value;
            }
        }


        public ChatTab(string tabHeading, TabControl ChatTabControl)
        {
            this.Header = closableTabHeader; // Assign the usercontrol to the tab header
            Grid ChatGrid = new Grid();
            this.AddChild(ChatGrid);
            this.tabTitle = tabHeading;
            this.Height = (((CloseableHeader)this.Header).FindName("label_TabTitle") as Label).Height;

            (((CloseableHeader)this.Header).FindName("button_close") as Button).Click += new RoutedEventHandler(button_close_Click); //click method for this button
            (((CloseableHeader)this.Header).FindName("label_TabTitle") as Label).SizeChanged += new SizeChangedEventHandler(label_TabTitle_SizeChanged); //event for size chnage 

            ChatGrid.ColumnDefinitions.Add(new ColumnDefinition() {Width = new GridLength(1, GridUnitType.Star) });
            ChatGrid.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(1, GridUnitType.Auto) });
            ChatGrid.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(1, GridUnitType.Star) });
            ChatGrid.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(1, GridUnitType.Auto) });
            ChatGrid.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(1, GridUnitType.Auto) });
            ChatGrid.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(1, GridUnitType.Auto) });

            Label ChatLabel = new Label();
            ChatLabel.Content = "- Chat History -";
            Grid.SetRow(ChatLabel, 0);
            ChatGrid.Children.Add(ChatLabel);

            ListBox ChatHistoryListBox = new ListBox();
            ChatHistoryListBox.BorderThickness = new System.Windows.Thickness(2);
            Grid.SetRow(ChatHistoryListBox, 1);
            ChatGrid.Children.Add(ChatHistoryListBox);
            
            ListBoxItem exampleChat = new ListBoxItem();
            exampleChat.Content = "Luke Says: Why hey there!";
            exampleChat.IsEnabled = false;
            ChatHistoryListBox.Items.Add(exampleChat);

            Label MessageLabel = new Label();
            MessageLabel.Content = "Message: ";
            Grid.SetRow(MessageLabel, 2);
            ChatGrid.Children.Add(MessageLabel);

            TextBox ChatMessageTextBox = new TextBox(); //Make a new message box
            ChatMessageTextBox.MaxHeight = 150;
            Grid.SetRow(ChatMessageTextBox, 3);
            ChatGrid.Children.Add(ChatMessageTextBox);

            Button SendMessageButton = new Button();
            SendMessageButton.Content = "Send Message";
            SendMessageButton.Width = 150;
            Grid.SetRow(SendMessageButton, 4);
            ChatGrid.Children.Add(SendMessageButton);
        }


        void button_close_Click(object sender, RoutedEventArgs e)
            //Remove the tab from the tab control
        {

            if (((TabControl)this.Parent).Items.Count - 1 == 0)
            {
                ((TabControl)this.Parent).Items.Add(new StartTab("Start Page", ((TabControl)this.Parent)));
            }

            ((TabControl)this.Parent).Items.Remove(this);

        }

        void label_TabTitle_SizeChanged(object sender, SizeChangedEventArgs e)
        // Label SizeChanged - When the Size of the Label changes (due to setting the Title) set position of button properly
        {
            (((CloseableHeader)this.Header).FindName("button_close") as Button).Margin = new Thickness(
            (((CloseableHeader)this.Header).FindName("label_TabTitle") as Label).ActualWidth + 5, 3, 4, 0);
        }

    }
}
